﻿
using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;
using System.Threading.Tasks;
//using Microsoft.Lync.Model;
using Microsoft.Lync;
using System.Runtime.InteropServices;
using Microsoft.Lync.Model;
using System.ComponentModel;




namespace Zune2LyncNP.src
{
    public sealed class LyncManager
    {
        private LyncClient cliente;
        public LyncManager()
        {
            try
            {
                cliente = LyncClient.GetClient();
                if (cliente.State == ClientState.SignedIn)
                    Conectado = true;
            }
            catch (Exception)
            {
                throw new LyncNotRunningException();

            }
            cliente.StateChanged += myClient_ClientStateChanged;

        }
        private bool _conectado;
        public bool Conectado { get { return _conectado; } set { _conectado = value; OnConectadoChanged(); } }

        public void CambiarNotaPersonal(string notaPersonal)
        {
            if (cliente.State == ClientState.SignedIn)
            {
                Dictionary<PublishableContactInformationType, object> publishData = new Dictionary<PublishableContactInformationType, object>();

                publishData.Add(PublishableContactInformationType.PersonalNote, notaPersonal);
                SendPublishRequest(publishData, "NotaPersonal");
            }
            else
            {
                throw new LyncNotSignedInException();
            }
        }
        public string ObtenerNotaPersonalActual()
        {
            if (cliente.State == ClientState.SignedIn)
            {
                try
                {
                    return cliente.Self.Contact.GetContactInformation(ContactInformationType.PersonalNote).ToString();
                }
                catch (Exception)
                {
                    return "";
                }
            }
            else
            {
                return "";
            }
            
        }

        private void myClient_ClientStateChanged(object source, ClientStateChangedEventArgs data)
        {
            if (data.NewState == ClientState.SignedIn)
            {
                Conectado = true;
                //MessageBox.Show("Signed in");
                return;
            }
            if (data.NewState == ClientState.SignedOut)
            {
                Conectado = false;
                return;
            }

            if (data.NewState == ClientState.ShuttingDown)
            {
                Conectado = false;
                return;
            }

            //MessageBox.Show(data.NewState.ToString());

        }
        /// <summary>
        /// Sends a publication request and handles any exceptions raised.
        /// </summary>
        /// <param name="publishData">Dictionary. Information to be published.</param>
        /// <param name="PublishId">string. Unique publication identifier.</param>
        private void SendPublishRequest(
           Dictionary<PublishableContactInformationType, object> publishData,
            string PublishId)
        {
            object publishState = (object)PublishId;
            object[] _PublishAsyncState = { cliente.Self, publishState };
            try
            {
                cliente.Self.BeginPublishContactInformation(
                    publishData,
                    null,
                    _PublishAsyncState);
            }
            catch (COMException ce)
            {
                //MessageBox.Show("publish COM exception: " + ce.ErrorCode.ToString());
            }
            catch (ArgumentException ae)
            {
                //MessageBox.Show("publish Argument exception: " + ae.Message);
            }
        }
        //varas de eventos

        public event EventHandler ConectadoChanged;
        private void OnConectadoChanged()
        {
            EventHandler handler = ConectadoChanged;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }
    }

    [Serializable]
    public class LyncNotRunningException : Exception
    {
        public LyncNotRunningException() { }
        public LyncNotRunningException(string message) : base(message) { }
        public LyncNotRunningException(string message, Exception inner) : base(message, inner) { }
        protected LyncNotRunningException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }
    [Serializable]
    public class LyncNotSignedInException : Exception
    {
        public LyncNotSignedInException() { }
        public LyncNotSignedInException(string message) : base(message) { }
        public LyncNotSignedInException(string message, Exception inner) : base(message, inner) { }
        protected LyncNotSignedInException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }
}
